package com.store.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.R;
import com.store.app.bean.SearchOrderBean;
import com.store.app.bean.SearchSubBean;
import com.store.app.utils.Utils;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/1.
 */

public class SearchOrderItemAdapter extends BaseAdapter {
    private List<SearchOrderBean> list;
    private Context context;

    public SearchOrderItemAdapter(Context context, List<SearchOrderBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {

        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        SearchOrderItemAdapter.ViewHolder viewHolder = null;
        SearchOrderBean searchOrderBean = list.get(position);
        if (convertView == null) {
            viewHolder = new SearchOrderItemAdapter.ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_search_order, null);
            viewHolder.tv_order_number = (TextView) convertView.findViewById(R.id.tv_order_number);
            viewHolder.tv_order_type = (TextView) convertView.findViewById(R.id.tv_order_type);
            viewHolder.lv_goods = (ListView) convertView.findViewById(R.id.lv_goods);
            viewHolder.tv_goods_count = (TextView) convertView.findViewById(R.id.tv_goods_count);
            viewHolder.tv_create_time = (TextView) convertView.findViewById(R.id.tv_create_time);
            viewHolder.tv_buyzer_name = (TextView) convertView.findViewById(R.id.tv_buyzer_name);
            viewHolder.tv_buyzer_phone = (TextView) convertView.findViewById(R.id.tv_buyzer_phone);
            viewHolder.tv_code = (TextView) convertView.findViewById(R.id.tv_code);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (SearchOrderItemAdapter.ViewHolder) convertView.getTag();
        }

        viewHolder.tv_order_number.setText(searchOrderBean.getOrder_no());
        viewHolder.tv_order_type.setText(searchOrderBean.getType());
        viewHolder.tv_goods_count.setText("共" + searchOrderBean.getItem_num() + "件商品");
        viewHolder.tv_create_time.setText(searchOrderBean.getOrder_date());
        viewHolder.tv_buyzer_name.setText(searchOrderBean.getContact_person());
        viewHolder.tv_buyzer_phone.setText(searchOrderBean.getContact_tel());
        viewHolder.tv_code.setText(searchOrderBean.getLoaded_code());
        List<SearchSubBean> searchSubBeanList = new ArrayList<>();
        String searchGoods = searchOrderBean.getGoods_list();
        try {
            JSONArray jsonArray = new JSONArray(searchGoods);
            Gson gson = new Gson();
            for(int i = 0;i<jsonArray.length();i++){
                SearchSubBean searchSubBean  =gson.fromJson(jsonArray.getJSONObject(i).toString(),SearchSubBean.class);
                searchSubBeanList.add(searchSubBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        viewHolder.lv_goods.setAdapter(new SearchSubAdapter(context,searchSubBeanList));
        Utils.Setlistview(viewHolder.lv_goods);
        return convertView;
    }


    class ViewHolder {
        TextView tv_order_number;//订单编号
        TextView tv_order_type;//订单类型
        ListView lv_goods;//商品列表
        TextView tv_goods_count;//共多少件商品
        TextView tv_create_time;//创建时间
        TextView tv_buyzer_name;//买家名字
        TextView tv_buyzer_phone;//买家手机号
        TextView tv_code;//提货码


    }
}
